package org.ihelpuoo.weboo.security.action;

import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.III;
import org.ihelpuoo.weboo.action.http.AjaxAction;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.module.AjaxException;
import org.ihelpuoo.weboo.security.Account;
import org.ihelpuoo.weboo.security.Session;

public class FetchSessionAccountAction extends AjaxAction
{
	@Override
	protected Object doAjax(ActionContext context) throws Exception
	{
		Session session = context.getSession();
		if (!session.isLogined())
			throw new AjaxException("security.nologin");
		else if (context.properties().getBoolean("cached"))
		{
			return session.getAccount();
		}

		Account newAccount = (Account)ObjectUtils.duplicate(session.getAccount());
		newAccount = (Account)context.pojo().fetch(newAccount);
		if (null == newAccount || newAccount.getId() <= III.DEFAULT_OBJECT_ID)
			throw new AjaxException("security.noexists");
		session.setAccount(newAccount);
		return newAccount;

	}
}
